'''
@project:pythonProjectT174
@auther:zhongyingjie
@file:broswersetting.py
@date:2022/11/7 10:48
@desc:
'''
from selenium import webdriver
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import unittest
from conf.config import config
from elements.tinyshop_elements import *

class broswersetting(unittest.TestCase):
    def setUp(self) -> None:
        if config.BROWSERNAME=='chrome':
            print(config.BROWSERNAME)
            self.driver = webdriver.Chrome()  # 创建一个浏览器对象
        elif config.BROWSERNAME=='firefox':
            print(config.BROWSERNAME)
            self.driver = webdriver.Firefox()
        elif config.BROWSERNAME=='edge':
            print(config.BROWSERNAME)
            self.driver = webdriver.Edge()
        self.driver.get(config.hdHost)
        self.driver.maximize_window()

        # self.driver.find_element(By.XPATH, '//a[text()="登录"]').click()
    def tearDown(self) -> None:
        self.driver.quit()

    # def baidu_sousuo(self,kw):
    #     self.driver.find_element(input_kw['element_locator'],input_kw['element_value']).send_keys(kw)
    #     self.driver.find_element(input_kw['element_locator'], input_kw['element_value']).click()
    #     self.driver.find_element(click_baidu['element_locator'], click_baidu['element_value']).send_keys(kw)
    #     self.driver.find_element(click_baidu['element_locator'], click_baidu['element_value']).click()
    # def baidu_news(self):
    #     self.driver.find_element(click_xw['element_locator'],click_xw['element_value']).click()
    # def baidu_hao123(self):
    #     self.driver.find_element(click_hao123['element_locator'],click_hao123['element_value']).click()
    # def baidu_tb(self):
    #     self.driver.find_element(click_tb['element_locator'],click_tb['element_value']).click()
    def zzdl(self,zh,mm):
        self.driver.find_element(click_dlym['element_locator'], click_dlym['element_value']).click()
        self.driver.find_element(input_yhm['element_locator'],input_yhm['element_value']).send_keys(zh)
        self.driver.find_element(input_mima['element_locator'], input_mima['element_value']).send_keys(mm)
        self.driver.find_element(click_dl['element_locator'], click_dl['element_value']).click()
    def qdss(self,ssc,zzjs):
        self.driver.implicitly_wait(3)
        self.driver.find_element(input_ss['element_locator'],input_ss['element_value']).send_keys(ssc)
        self.driver.find_element(click_ss['element_locator'],click_ss['element_value']).click()
        self.driver.find_element(click_sp['element_locator'],click_sp['element_value']).click()
        self.driver.find_element(click_ys['element_locator'],click_ys['element_value']).click()
        self.driver.find_element(click_cm['element_locator'],click_cm['element_value']).click()
        if zzjs=='加入购物车':
            self.driver.find_element(click_jrgwc['element_locator'],click_jrgwc['element_value']).click()
            gwc = self.driver.find_element(click_gwc['element_locator'],click_gwc['element_value'])
            mo = ActionChains(self.driver)
            mo.move_to_element(gwc).perform()
            self.driver.find_element(click_gwcjs['element_locator'], click_gwcjs['element_value']).click()
        elif zzjs=='立即购买':
            self.driver.find_element(click_ljgm['element_locator'], click_ljgm['element_value']).click()
        self.driver.find_element(click_ljjs['element_locator'], click_ljjs['element_value']).click()
        self.driver.find_element(click_tjdd['element_locator'], click_tjdd['element_value']).click()
        self.driver.find_element(click_ljzf['element_locator'], click_ljzf['element_value']).click()
        ifzf = self.driver.window_handles
        self.driver.switch_to.window(ifzf[1])

        # 后台登录
    def tiny_htzh(self, name, password, verifyCode):
        self.driver.find_element(input_htzh['element_locator'],
                                 input_htzh['element_value']).send_keys(name)
        self.driver.find_element(input_htmm['element_locator'],
                                 input_htmm['element_value']).send_keys(password)
        self.driver.find_element(input_htyz['element_locator'],
                                 input_htyz['element_value']).send_keys(verifyCode)
        self.driver.find_element(click_htdl['element_locator'],
                                 click_htdl['element_value']).click()

#添加商品
    def tiny_tj(self,name,goods_no,pro_no,store_nums,sell_price,market_price,cost_price):
        self.driver.implicitly_wait(2)
        self.driver.find_element(click_spzx['element_locator'],
                                 click_spzx['element_value']).click()
        self.driver.find_element(click_tj['element_locator'],
                                 click_tj['element_value']).click()
        self.driver.find_element(click_fl['element_locator'],
                                 click_fl['element_value']).click()
        self.driver.find_element(click_fl1['element_locator'],
                                 click_fl1['element_value']).click()
        self.driver.find_element(input_mc['element_locator'],
                                 input_mc['element_value']).send_keys(name)
        self.driver.find_element(input_bh['element_locator'],
                                 input_bh['element_value']).send_keys(goods_no)
        self.driver.find_element(click_qhkj['element_locator'],
                                 click_qhkj['element_value']).click()
        iframe=self.driver.find_element(i_frame['element_locator'],
                                 i_frame['element_value'])
        self.driver.switch_to.frame(iframe)

        self.driver.find_element(click_tk['element_locator'],
                                 click_tk['element_value']).click()
        self.driver.find_element(click_xztp['element_locator'],#选择图片
                                 click_xztp['element_value']).click()
        self.driver.find_element(click_tjtp['element_locator'],
                                 click_tjtp['element_value']).click()
        self.driver.switch_to.default_content()
        self.driver.find_element(input_gg1['element_locator'],
                                 input_gg1['element_value']).send_keys(pro_no)
        self.driver.find_element(input_gg2['element_locator'],
                                 input_gg2['element_value']).send_keys(store_nums)
        self.driver.find_element(input_gg3['element_locator'],
                                 input_gg3['element_value']).send_keys(sell_price)
        self.driver.find_element(input_gg4['element_locator'],
                                 input_gg4['element_value']).send_keys(market_price)
        self.driver.find_element(input_gg5['element_locator'],
                                 input_gg5['element_value']).send_keys(cost_price)
        self.driver.find_element(click_tijiao['element_locator'],
                                 click_tijiao['element_value']).click()


    #添加用户
    def tjyh(self,yhm,mm,qrmm,yx,zsxm,sr):
        self.driver.implicitly_wait(3)
        self.driver.find_element(click_khzx['element_locator'],
                                 click_khzx['element_value']).click()
        self.driver.find_element(click_tjyhan['element_locator'],
                                 click_tjyhan['element_value']).click()
        self.driver.find_element(input_tjyhm['element_locator'],
                                 input_tjyhm['element_value']).send_keys(yhm)
        self.driver.find_element(input_tjmm['element_locator'],
                                 input_tjmm['element_value']).send_keys(mm)
        self.driver.find_element(input_tjqrmm['element_locator'],
                                 input_tjqrmm['element_value']).send_keys(qrmm)
        self.driver.find_element(input_tjyx['element_locator'],
                                 input_tjyx['element_value']).send_keys(yx)
        self.driver.find_element(input_tjyhm['element_locator'],
                                 input_tjyhm['element_value']).send_keys(yhm)
        self.driver.find_element(input_tjzxxm['element_locator'],
                                 input_tjzxxm['element_value']).send_keys(zsxm)
        self.driver.find_element(input_tjsr['element_locator'],
                                 input_tjsr['element_value']).send_keys(sr)
        se = self.driver.find_element(select_shen['element_locator'],
                                 select_shen['element_value'])
        se1 = self.driver.find_element(select_shi['element_locator'],
                                 select_shi['element_value'])
        se2 = self.driver.find_element(select_xian['element_locator'],
                                 select_xian['element_value'])
        s = Select(se)
        s1 = Select(se1)
        s2 = Select(se2)
        s.select_by_visible_text('湖南省')
        s1.select_by_visible_text('株洲市')
        s2.select_by_visible_text('芦淞区')
        self.driver.find_element(click_tjyhsq['element_locator'],
                                 click_tjyhsq['element_value']).click()

    def djj(self, name, mz, xfje, jf):  # 代金券添加
        self.driver.find_element(click_yxtg['element_locator'], click_yxtg['element_value']).click()
        self.driver.find_element(click_djjmb['element_locator'], click_djjmb['element_value']).click()
        self.driver.find_element(click_tjdjj['element_locator'], click_tjdjj['element_value']).click()
        self.driver.find_element(input_djjname['element_locator'], input_djjname['element_value']).send_keys(name)
        self.driver.find_element(input_mz['element_locator'], input_mz['element_value']).send_keys(mz)
        self.driver.find_element(input_xfje['element_locator'], input_xfje['element_value']).send_keys(xfje)
        self.driver.find_element(input_jf['element_locator'], input_jf['element_value']).clear()
        self.driver.find_element(input_jf['element_locator'], input_jf['element_value']).send_keys(jf)
        self.driver.find_element(click_djjtj['element_locator'], click_djjtj['element_value']).click()

    def djjbj(self, name, mz, xfje):
        self.driver.find_element(click_yxtg['element_locator'], click_yxtg['element_value']).click()
        self.driver.find_element(click_djjmb['element_locator'], click_djjmb['element_value']).click()
        self.driver.find_element(click_CL['element_locator'], click_CL['element_value']).click()
        self.driver.find_element(click_bjdjj['element_locator'], click_bjdjj['element_value']).click()
        self.driver.find_element(input_djjname['element_locator'], input_djjname['element_value']).clear()  # 清空模板名字输入框
        self.driver.find_element(input_djjname['element_locator'], input_djjname['element_value']).send_keys(name)
        self.driver.find_element(input_mz['element_locator'], input_mz['element_value']).clear()  # 清空面值框
        self.driver.find_element(input_mz['element_locator'], input_mz['element_value']).send_keys(mz)
        self.driver.find_element(input_xfje['element_locator'], input_xfje['element_value']).clear()  # 清空消费金额
        self.driver.find_element(input_xfje['element_locator'], input_xfje['element_value']).send_keys(xfje)
        self.driver.find_element(click_djjtj['element_locator'], click_djjtj['element_value']).click()  # 点击提交

    def cjdjj(self, stime, etime, num):  # 创建代金券
        self.driver.find_element(click_yxtg['element_locator'], click_yxtg['element_value']).click()
        self.driver.find_element(click_djjmb['element_locator'], click_djjmb['element_value']).click()
        self.driver.find_element(click_CL['element_locator'], click_CL['element_value']).click()
        self.driver.find_element(click_cjdjj['element_locator'], click_cjdjj['element_value']).click()  # 点击创建代金券
        self.driver.find_element(input_starttime['element_locator'], input_starttime['element_value']).send_keys(
            stime)  # 输入开始时间
        self.driver.find_element(click_kongbai['element_locator'], click_kongbai['element_value']).click()  # 点击空白处
        self.driver.find_element(input_endtime['element_locator'], input_endtime['element_value']).send_keys(
            etime)  # 输入结束时间
        self.driver.find_element(input_num['element_locator'], input_num['element_value']).send_keys(num)  # 输入张数
        self.driver.find_element(click_sc['element_locator'], click_sc['element_value']).submit()  # 点击生成

    def scdjj(self):
        # 删除代金券模板
        self.driver.find_element(click_yxtg['element_locator'], click_yxtg['element_value']).click()
        self.driver.find_element(click_djjmb['element_locator'], click_djjmb['element_value']).click()
        self.driver.find_element(click_CL['element_locator'], click_CL['element_value']).click()
        self.driver.find_element(click_dele['element_locator'], click_dele['element_value']).click()  # 点击删除
        self.driver.find_element(click_deqr['element_locator'], click_deqr['element_value']).click()  # 点击确认

    def ddfh(self, dhnum):
        self.driver.find_element(click_ddzx['aa'], click_ddzx['bb']).click()
        time.sleep(1)
        self.driver.find_element(click_ddcl['aa'], click_ddcl['bb']).click()
        time.sleep(1)
        self.driver.find_element(click_fh['aa'], click_fh['bb']).click()

        time.sleep(2)
        y = self.driver.find_element(click_frme['aa'], click_frme['bb'])
        self.driver.switch_to.frame(y)
        time.sleep(1)
        self.driver.find_element(input_dh['aa'], input_dh['bb']).send_keys(dhnum)
        time.sleep(1)
        self.driver.find_element(click_djfh['aa'], click_djfh['bb']).click()

    def ddfh1(self):  # 未输入单号
        self.driver.find_element(click_ddzx['aa'], click_ddzx['bb']).click()
        time.sleep(1)
        self.driver.find_element(click_ddcl2['aa'], click_ddcl2['bb']).click()
        time.sleep(1)
        self.driver.find_element(click_fh['aa'], click_fh['bb']).click()
        time.sleep(1)
        y = self.driver.find_element(click_frme['aa'], click_frme['bb'])
        self.driver.switch_to.frame(y)
        time.sleep(1)
        self.driver.find_element(click_djfh['aa'], click_djfh['bb']).click()

    def tinyshop_dz(self,zip,addr,accept_name,mobile,phone):
        ActionChains(self.driver).release(self.driver.find_element(action_center['element_locator'],action_center['element_value'])).perform()
        self.driver.find_element(click_sh['element_locator'],click_sh['element_value']).click()
        self.driver.find_element(click_ao['element_locator'],click_ao['element_value']).click()
        iframe=self.driver.find_element(i_frame1['element_locator'],i_frame1['element_value'])
        self.driver.switch_to.frame(iframe)
        self.driver.find_element(click_zip1['element_locator'],click_zip1['element_value']).click()
        self.driver.find_element(click_zip2['element_locator'],click_zip2['element_value']).click()
        self.driver.find_element(click_zip3['element_locator'],click_zip3['element_value']).click()
        self.driver.find_element(input_zip['element_locator'],input_zip['element_value']).send_keys(zip)
        self.driver.find_element(input_addr['element_locator'],input_addr['element_value']).send_keys(addr)
        self.driver.find_element(input_an['element_locator'],input_an['element_value']).send_keys(accept_name)
        self.driver.find_element(input_mobile['element_locator'],input_mobile['element_value']).send_keys(mobile)
        self.driver.find_element(input_phone['element_locator'],input_phone['element_value']).send_keys(phone)
        self.driver.find_element(click_dz['element_locator'],click_dz['element_value']).click()